﻿var typeData = [];
$.ajax({
    url: "/Admin/ProductCategory/GetList",
    type: "POST",
    async: false,
    success: function (data) {
        typeData = data;
    }
})
$("#Main").datagrid({
    url: "/Admin/Product/GetList",
    fit: true,
    fitColumns: true,
    striped: true,
    rownumbers: true,
    remoteSort: false,
    singleSelect: true,
    idField: "ID",
    toolbar: [{
        text: '添加商品',
        iconCls: 'icon-add',
        handler: function () { add(); }
    }],
    columns: [[{
        title: "商品名称",
        field: "ProductName",
        width: 15
    }, {
        title: "商品分类",
        field: "CatID",
        width: 15,
        formatter: function (value, row, index) {
            for (x in typeData) {
                if (typeData[x].ID == $.trim(value))
                    return typeData[x].CatName;
            }
            return "分类错误";
        }
    }, {
        title: "商品价格",
        field: "Price",
        width: 48
    }, {
        title: "排序",
        field: "Sort",
        width: 10
    }, {
        field: 'action',
        title: '编辑',
        width: 12,
        align: 'center',
        formatter: function (value, row, index) {
            return '<a href="#" onclick="edit(' + row.ID + ')">编辑</a>&nbsp;|&nbsp;<a href="#" onclick="del(' + row.ID + ')">删除</a>';
        }
    }]]
});
function add() {
    location.href = "/Admin/Product/Create?current=5&r=" + Math.random();
}
function edit(id) {
    location.href = "/Admin/Product/Edit/" + id + "?current=5&r=" + Math.random();
}
function del(id) {
    $.messager.confirm('警告', '确定删除?', function (r) {
        if (r)
            location.href = "/Admin/Product/Delete/" + id + "?current=5&r=" + Math.random();
    });
}